export default function createRouteMap(routes, oldPathMap) {
  let pathMap = oldPathMap || Object.create(null)

  routes.forEach(route => {
    addRouteRecord(route, pathMap)
  })
  return pathMap
}

// 先序深度
function addRouteRecord(route, pathMap, parent) {
  let { path, component } = route
  path = parent ? parent.path + '/' + path : path
  let record = {
    path,
    component,
    parent
  }
  if(!pathMap[path]) {
    pathMap[path] = record
  }

  if(route.children) {
    route.children.forEach(childRoute => {
      // 在遍历儿子时 将父亲的记录传入
      addRouteRecord(childRoute, pathMap, record)
    })
  }
}
